package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.activity.LiveDetailsPage;
import com.example.administrator.liveapp.activity.LoginPage;
import com.example.administrator.liveapp.activity.SecondaryComments;
import com.example.administrator.liveapp.activity.WxLoginPage;
import com.example.administrator.liveapp.bean.LiveDetailsBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.TimeChangeUtil;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/7 0007.
 */

public class LiveDetailCommentAdapter extends RecyclerView.Adapter<LiveDetailCommentAdapter.ViewHolder> implements View.OnClickListener{
    public Context context;
    private List<LiveDetailsBean.CommentBean> live_content;

    public Secondary_comments sc;

    public LiveDetailCommentAdapter(List<LiveDetailsBean.CommentBean> live_content) {
        this.live_content = live_content;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.live_details_item, parent, false);
        context = parent.getContext();
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    int count = 0;
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (live_content.size() > 0) {
            holder.comment_content.setText(live_content.get(position).getCommentcontent());
            if(live_content.get(position).getConsumerphoto()!=null&&!live_content.get(position).getConsumerphoto().equals("")){
                String str = live_content.get(position).getConsumerphoto();
                String photo = str.substring(0,4);
                if(photo.equals("http")){
                    Glide.with(context)
                            .load(live_content.get(position).getConsumerphoto())
                            .error(R.mipmap.consumer)
                            .into(holder.comments_head_portrait);
                }else{
                    Glide.with(context)
                            .load(AppUrl.URLS + live_content.get(position).getConsumerphoto())
                            .error(R.mipmap.consumer)
                            .into(holder.comments_head_portrait);
                }
            }
            holder.user_name.setText(live_content.get(position).getConsumername());
            holder.comments_time.setText(TimeChangeUtil.getStrTime3(live_content.get(position).getCommenttime()));
            holder.text_number.setText(live_content.get(position).getCommentlikecount());
            holder.commentcount.setText(live_content.get(position).getCommentcount());
            if(live_content.get(position).getCommentBestStatics().equals("1")){
                holder.commentlikecount.setImageResource(R.drawable.praise);
            }else if(live_content.get(position).getCommentBestStatics().equals("0")){
                holder.commentlikecount.setImageResource(R.drawable.praise_w);
            }
            holder.commentlikecount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ApplicationUtils.getInstance().loginBean!=null){
                        String url = AppUrl.URLS + AppUrl.LikeUrl;
                        Map<String, String> map = new HashMap<>();
                        map.put("likeTid", "3");
                        map.put("likeUid", ApplicationUtils.getInstance().loginBean.getConsumerid());
                        map.put("likeFid", live_content.get(position).getCommentid());
                        XUtils.Get(url, map, new MyCallBack<String>() {
                            @Override
                            public void onSuccess(String result) {
                                super.onSuccess(result);
                                JSONObject jsonObject = null;
                                try {
                                    jsonObject = new JSONObject(result);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String code = jsonObject.optString("code");
                                if (code.equals("7600") || code.equals("7930")) {
                                    count = 1;
                                } else if (code.equals("7900")) {
                                    count = 2;
                                } else {
                                    Toast.makeText(context, code + "点赞失败", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                super.onError(ex, isOnCallback);
                                Toast.makeText(context, "点赞错误", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFinished() {
                                super.onFinished();
                                int likes = Integer.parseInt(holder.text_number.getText().toString());
                                if (count == 1) {
                                    holder.text_number.setText(likes + 1 + "");
                                    holder.commentlikecount.setImageResource(R.drawable.praise);
                                } else if (count == 2) {
                                    holder.text_number.setText(likes - 1 + "");
                                    holder.commentlikecount.setImageResource(R.drawable.praise_w);
                                }

                            }
                        });
                    }else{
                        Toast.makeText(context, "用户未登录，请登录后点赞", Toast.LENGTH_SHORT).show();
                        context.startActivity(new Intent(context,WxLoginPage.class));
                    }

                }
            });

            holder.commentcount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ApplicationUtils.getInstance().loginBean!=null){
                        Intent intent = new Intent(context, SecondaryComments.class);
                        intent.putExtra("position",live_content.get(position).getCommentid());
                        intent.putExtra("name",live_content.get(position).getConsumername());
                        context.startActivity(intent);
                    }else{
                        Toast.makeText(context, "用户未登录，请登录后评论", Toast.LENGTH_SHORT).show();
                        context.startActivity(new Intent(context,WxLoginPage.class));
                    }

                }
            });

            //二级评论:
            FullyLinearLayoutManager mLinearLayoutManager = new FullyLinearLayoutManager(context);
            mLinearLayoutManager.setScrollEnabled(false);
            holder.secondary_recycler.setLayoutManager(mLinearLayoutManager);
            holder.secondary_recycler.setHasFixedSize(true);
            sc = new Secondary_comments(live_content.get(position).getReply());
            holder.secondary_recycler.setAdapter(sc);
        }
    }

    class Secondary_comments extends RecyclerView.Adapter<Secondary_comments.ViewHolder> {
        public Context context;
        public List<LiveDetailsBean.CommentBean.ReplyBean> reply;
        public Secondary_comments(List<LiveDetailsBean.CommentBean.ReplyBean> reply) {
            this.reply = reply;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.secondary_comments_item, null);
            ViewHolder viewHolder = new ViewHolder(view);
            context = parent.getContext();
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.reply_comments.setText(reply.get(position).getCommentcontent());
            holder.secondary_name.setText(reply.get(position).getConsumername()+"回复");
            holder.secondary_time.setText(TimeChangeUtil.getStrTime3(reply.get(position).getCommenttime()));
        }


        @Override
        public int getItemCount() {
            if(reply!=null){
                return reply.size();
            }else{
                return 0;
            }

        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView reply_comments;
            public TextView secondary_name;
            public TextView secondary_time;
            public ViewHolder(View itemView) {
                super(itemView);
                reply_comments = (TextView) itemView.findViewById(R.id.reply_comments);
                secondary_name = (TextView) itemView.findViewById(R.id.secondary_name);
                secondary_time = (TextView) itemView.findViewById(R.id.secondary_time);
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    @Override
    public int getItemCount() {
        if (live_content == null) {
            return 0;
        } else {
            return live_content.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView comments_head_portrait;
        public TextView user_name;
        public TextView comments_time;
        public RadioButton commentcount;
        public ImageView commentlikecount;
        public TextView comment_content;
        public TextView text_number;
        public RecyclerView secondary_recycler;

        public ViewHolder(View itemView) {
            super(itemView);
            secondary_recycler = (RecyclerView) itemView.findViewById(R.id.secondary_recycler);
            comments_head_portrait = (ImageView) itemView.findViewById(R.id.comments_head_portrait);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            comments_time = (TextView) itemView.findViewById(R.id.comments_time);
            commentcount = (RadioButton) itemView.findViewById(R.id.article_details_watch_number);
            commentlikecount = (ImageView) itemView.findViewById(R.id.article_details_Thumb_number);
            comment_content = (TextView) itemView.findViewById(R.id.comment_content);
            text_number = (TextView) itemView.findViewById(R.id.text_number);
        }
    }
}
